<template>
	<view>
		<view class="headView bg-gradual-green">
			<view class="backButton"><text class="cuIcon-back" @click="navBack"></text></view>
			<view class="headContent">

				<view class="texts">
					<view class="title1"><b>助田园</b></view>
					<view class="title2">与你一起为乡村振兴助力!</view>
				</view>

				<view class="img">
					<image src="../../static/vegetable.png" style="width: 200upx;height: 200upx;" mode=""></image>
				</view>
			</view>
		</view>
		<view class="formView">
			<view class="formLabel">会员{{this.isLoginType ? '登录' : '注册'}}</view>
			<view class="loginForm">
				<view class="cu-form-group formItems">
					<uni-icons type="phone-filled" color="#8dc63f" size="22"></uni-icons>
					<input v-model="account.openId" @blur="checkExist()" style="margin-left: 20upx;" placeholder="手机号" name="input"
					 type="number"></input>
				</view>

				<view class="cu-form-group formItems">
					<uni-icons type="locked-filled" color="#8dc63f" size="22"></uni-icons>
					<input v-model="account.password" style="margin-left: 20upx;" password="" placeholder="密码" name="input" type="number"></input>
				</view>

				<view v-if="!isLoginType" class="cu-form-group formItems">
					<uni-icons type="locked-filled" color="#8dc63f" size="22"></uni-icons>
					<input v-model="account.confirmPwd" style="margin-left: 20upx;" password="" placeholder="确认密码" name="input" type="number"></input>
				</view>

				<view v-if="!isLoginType" class="cu-form-group formItems">
					<uni-icons type="person-filled" color="#8dc63f" size="22"></uni-icons>
					<input v-model="account.realName" style="margin-left: 20upx;" placeholder="真实姓名" name="input"></input>
				</view>

				<view class="extra text-green">
					<view class="switichType" @click="switichPageType">{{this.isLoginType ? '前往注册' : '返回登录'}}</view>
					<view class="forget">忘记密码</view>
				</view>

				<view class="buttonArea">
					<button :loading="isloading" class="cu-btn lg  bg-green" style="width: 400upx;" @click="doSubmit">立即{{this.isLoginType ? '登录' : '注册'}}</button>
				</view>



			</view>
		</view>




	</view>
</template>

<script>
	import uniIcons from '../../components/uni-icons/uni-icons.vue'
	import {
		doLogin,
		doRegister,
		checkExist,
		getUserInfoByOpenId
	} from '../../api/userAPI.js'
	export default {
		components: {
			uniIcons
		},
		data() {
			return {
				isLoginType: true,
				account: {
					openId: '',
					password: '',
					confirmPwd: '',
					realName: '',
				},
				user:{},
				isloading: false,
				hot: 0,
			}
		},
		onLoad(params) {
			const app = getApp()
			const user = uni.getStorageSync('user')
			console.log("user: ",user);
			if('' != user.userOpenid) {
				console.log('1');
				this.user = user
				this.hot = params.hot
				this.account.openId = user.userOpenId
			}
			
			
		},
		onReady() {
			if (this.hot == 1) {
				let auth = [false, false]
				uni.checkIsSoterEnrolledInDevice({
					checkAuthMode: 'fingerPrint',
					success(res) {
						auth[0] = res.isEnrolled;
					}
				})
				uni.checkIsSoterEnrolledInDevice({
					checkAuthMode: 'facial',
					success(res) {
						auth[1] = res.isEnrolled;
					}
				})

				if (auth[0]) {
					let that = this;
					uni.startSoterAuthentication({
						requestAuthModes: ['fingerPrint'],
						authContent: '请使用指纹登录',
						challenge: this.account.openId,
						success:(res)=> {
							this.isLoginType = true
							console.log("this.user: ",this.user);
							this.account.openId = this.user.userOpenid
							this.account.password = this.user.userPassword
							this.doSubmit()
							// uni.showToast({
							// 	title: '登录成功',
							// 	duration: 2000
							// })
							
							// uni.setStorageSync({
							// 	key: 'lastLoginTime',
							// 	data: new Date().getTime()
							// })
							// setTimeout(() => {
							// 	uni.navigateBack({
							// 		delta: 1
							// 	})
							// }, 1500)

							return 0;
						},
						fail(res) {
							return 0;
						}
					})
				}

				if (auth[1]) {
					uni.startSoterAuthentication({
						requestAuthModes: ['facial'],
						authContent: '请使用FaceID登录',
						challenge: this.account.openId,
						success: (res) => {
							this.isLoginType = true
							this.account.openId = this.user.userOpenid
							this.account.password = this.user.userPassword
							this.doSubmit()
							// uni.showToast({
							// 	title: '登录成功',
							// 	duration: 2000
							// })
							// uni.setStorageSync('lastLoginTime',new Date().getTime())
							// setTimeout(() => {
							// 	uni.navigateBack({
							// 		delta: 1
							// 	})
							// }, 1500)
							
							return 0;

						}
					})
				}


			}
		},
		methods: {
			doSubmit() {
				if (this.isLoginType) {
					this.doLogin()
				} else {
					this.doRegister()
				}
			},

			doLogin() {
				console.log("this.account: ", JSON.stringify(this.account));
				if ('' == this.account.openId || '' == this.account.password) {
					uni.showModal({
						showCancel: false,
						title: '提示',
						content: '请确认已填写手机号和密码'
					})
					return false;
				}
				this.isloading = true
				uni.showLoading({
					title: '正在处理',
					mask: true
				});
				setTimeout(() => {
					doLogin(this.account.openId, this.account.password).then(resp => {
						if ("true" == resp.result) {
							uni.hideLoading()
							uni.showToast({
								title: '登录成功',
								duration: 2000
							})
							// uni.showToast({
							// 	title: '欢迎使用助田园！',
							// 	position: 'bottom'
							// })
							this.getUserInfo(this.account.openId)
						} else {
							uni.hideLoading()
							uni.showModal({
								showCancel: false,
								title: '登陆失败',
								content: '请检查手机号或密码是否正确'
							})
						}
					})
					this.isloading = false
				}, 2500)

			},
			doRegister() {
				if ('' == this.account.openId || '' == this.account.password ||
					'' == this.account.confirmPwd || '' == this.account.realName) {
					uni.showModal({
						showCancel: false,
						title: '提示',
						content: '请确认已填写完成注册信息'
					})
					return false;
				}

				this.checkExist()

				if (this.account.password != this.account.confirmPwd) {
					uni.showModal({
						showCancel: false,
						title: '提示',
						content: '两次密码输入不一致，请重新输入！'
					})
					this.account.password = ''
					this.account.confirmPwd = ''
					return false;
				}

				doRegister(this.account.openId, this.account.password, this.account.realName).then(resp => {
					if ('true' == resp.result) {
						uni.showToast({
							title: '注册成功'
						});
					}
					this.switichPageType()
				})



			},
			checkExist() {
				if ('' == this.account.openId)
					return false;

				if (!this.isLoginType) {
					checkExist(this.account.openId).then(resp => {
						if (resp != null) {
							this.account.openId = '',
								uni.showModal({
									showCancel: false,
									title: '提示',
									content: '该手机号已被注册'
								})
							return false;
						}
					})
				}
				return true;
			},
			getUserInfo(openId) {
				const app = getApp();
				checkExist(openId).then(resp => {
					console.log("resp: ", resp);
					app.globalData.user = resp
					uni.setStorageSync('user',resp)
				})
				
				

				uni.setStorage({
					key: 'lastLoginTime',
					data: new Date().getTime(),
					success() {
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 500)
					}
				})

				// uni.navigateBack({
				// 	delta:1
				// })
			},

			switichPageType() {
				this.isLoginType = !this.isLoginType
			},
			navBack() {
				uni.switchTab({
					url:'../index/index'
				})
			}
		}
	}
</script>

<style>
	@import url("./login.css");
</style>
